<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 * User: jinzhao
 * Date: 16-8-13
 * Time: 下午4:12
 */
class Template extends MY_Controller{
    public function create()
    {
        $this->load->model('template_model');
        $this->load->helper('set');
        
        $cup_template = $this->template_model->get_cup_template();
        $data['cup_template'] = $cup_template;
        $data['email'] = $this->session->userdata('email');
        $this->load->view('create_8583.html', $data);
    }
    
    public function create_result()
    {
        $this->load->model('template_model');
        if ($this->template_model->create() == false){
            $data = array(
              'errmsg' => $this->template_model->display_errors()  
            );
            $this->load->view('create_8583.html', $data);
            return;
        }
        $data = array(
            'title' => '创建模板',
            'msg' => '创建成功'
        );
            
        $this->load->view('success.html', $data);
    }
    public function get()
    {
        $id = $this->input->post('id');
        
        $this->load->model('template_model');
        $result = $this->template_model->get_by_id($id);
        if (!$result || count($result) == 0){
            $data['code'] = 'error';
        }else{
            $data['code'] = 'ok';
            $result[0]['content'] = json_decode($result[0]['content']);
            $data['template_info'] = $result[0];
        }
        $output = json_encode($data);
        echo $output;
    }
    
    public function update()
    {

        $this->load->model('template_model');
        $result = $this->template_model->update();
        if (!$result){
            $data['code'] = 'error';
            $data['msg'] = $this->template_model->display_errors();
        }else {
            $data['code'] = 'ok';
        }
        $output = json_encode($data);
        echo $output;
    }
    
    function delete()
    {
        $id = $this->input->post('id');
        
        $this->load->model('template_model');
        $result = $this->template_model->delete($id);
        if ($result){
           $data['code'] = 'ok';
        }else{
            $data['code'] = 'error';
        }
        echo json_encode($data);
    }
    
}